﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Produce
{
    public partial class ChanPin_PHB : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Panel2.Visible = true;
        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.SqlDataSource3.SelectCommand = "SELECT [ID], [品名]+' '+ [规格]+'('+[单位]+')' as [品名规格] FROM [原料]";
            this.YuanFuLiao.DataBind();
        }

        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            this.SqlDataSource3.SelectCommand = "SELECT [ID], [品名]+' '+ [规格]+'('+[单位]+')' as [品名规格] FROM [辅料]";
            this.YuanFuLiao.DataBind();
        }

        protected void Button_Save0_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            float phb=0;
            float.TryParse(this.PeiHeBi.Text, out phb);
            if (phb <= 0)
            {
                this.Message.Text = "·配合比输入有误";
                return;
            }
            string yl;
            if(this.RadioButton1.Checked)
            {
                yl="1";
            }
            else
            {
                yl="0";
            }
            this.SqlDataSource3.UpdateCommand = "if not exists(select * from [产品原辅料配合比] where [产品ID]=" 
                + this.ChanPin.Text + " and [原辅料ID]=" + this.YuanFuLiao.Text + " and [是原料]=" 
                + yl + ") begin insert into [产品原辅料配合比]([产品ID],[原辅料ID],[是原料],[配合比])values("
                +this.ChanPin.Text+","+this.YuanFuLiao.Text+","+yl+","+phb+") end";
            //Response.Write(this.SqlDataSource3.UpdateCommand);
            if (this.SqlDataSource3.Update() == 1)
            {
                this.Message.Text = "·新增产品原辅料配合比成功";
                this.Panel2.Visible = false;
                this.GridView1.DataBind();
            }
            else
            {
                this.Message.Text = "·该配合比已经存在，新增失败";
            }
            
            
        }
    }
}
